﻿using System;
using System.Collections.Generic;
using System.Web;

namespace Manager.CustomerHelper
{
    /// <summary>
    /// 模块的权限列表
    /// </summary>
    public enum ModulePowerType
    {
        None,

        CanRead,
        //CanDelete,
        //CanEdit,
        //CanAdd,
        
        CoreMenu_New,
        CoreMenu_Edit,
        CoreMenu_Delete,


        CoreJobTitle_New,
        CoreJobTitle_Edit,
        CoreJobTitle_Delete,


        CoreRole_New,
        CoreRole_Edit,
        CoreRole_Delete,

        CoreUser_New,
        CoreUser_Edit,
        CoreUser_Delete,
        CoreUser_ChangePassword,

        CoreRoleUser_New,
        CoreRoleUser_Delete,

        CoreRoleModule_Edit,

        CoreRoleMenu_Edit,

        CoreLog_Delete,

        CoreConf_Edit,

        CorePassword_Edit,

    }

    public class ModulePowerTypeHelper
    {

        /// <summary>
        /// 其他权限的标题
        /// </summary>
        /// <param name="powerName"></param>
        /// <returns></returns>
        public static string GetTitle(string moduleName, string powerName)
        {
            return GetTitle(String2Power(String.Format("{0}_{1}", moduleName, powerName)));
        }

        /// <summary>
        /// 其他权限的标题
        /// </summary>
        /// <param name="power"></param>
        /// <returns></returns>
        public static string GetTitle(ModulePowerType power)
        {
            string result = String.Empty;

            string powerTypeName = ModulePowerTypeHelper.GetPowerTypeName(power);
            if (powerTypeName == "New")
            {
                result = "新增";
            }
            else if (powerTypeName == "Delete")
            {
                result = "删除";
            }
            else if (powerTypeName == "Edit")
            {
                result = "编辑";
            }
            else if (powerTypeName == "ShenHe")
            {
                result = "审核";
            }
            else if (powerTypeName == "Send")
            {
                result = "发送";
            }


            else if (powerTypeName == "EditA")
            {
                result = "正式修改";
            }
            else if (powerTypeName == "NewA")
            {
                result = "正式新增";
            }
            else if (powerTypeName == "DeleteA")
            {
                result = "正式删除";
            }
            else if (powerTypeName == "AnnexA")
            {
                result = "正式附件";
            }
            else if (powerTypeName == "DownA")
            {
                result = "正式下载";
            }
            else if (powerTypeName == "FolderA")
            {
                result = "正式目录";
            }



            else if (powerTypeName == "EditB")
            {
                result = "共享修改";
            }
            else if (powerTypeName == "NewB")
            {
                result = "共享新增";
            }
            else if (powerTypeName == "DeleteB")
            {
                result = "共享删除";
            }
            else if (powerTypeName == "AnnexB")
            {
                result = "共享附件";
            }
            else if (powerTypeName == "DownB")
            {
                result = "共享下载";
            }
            else if (powerTypeName == "FolderB")
            {
                result = "共享目录";
            }


            else if (powerTypeName == "Lend")
            {
                result = "借调管理";
            }



            else if (powerTypeName == "Caicui")
            {
                result = "采购跟催";
            }

            else if (powerTypeName == "Caigen")
            {
                result = "采购跟进";
            }




            else if (powerTypeName == "Down")
            {
                result = "下载";
            }


            else if (powerTypeName == "Guest")
            {
                result = "访客";
            }

            else if (powerTypeName == "Restaurant")
            {
                result = "食堂";
            }
            else if (powerTypeName == "MenuAdmin")
            {
                result = "菜单";
            }
            else if (powerTypeName == "DutyCom")
            {
                result = "工业园班";
            }
            else if (powerTypeName == "DutyStone")
            {
                result = "矿物园班";
            }
            else if (powerTypeName == "DutyRestaurant")
            {
                result = "招待所班";
            }
            else if (powerTypeName == "Tshow")
            {
                result = "人物秀";
            }
            else if (powerTypeName == "Ashow")
            {
                result = "公告";
            }
            else
            {
                switch (power)
                {
                    case ModulePowerType.CoreUser_ChangePassword:
                        result = "修改密码";
                        break;
                }
            }

            return result;
        }

        /// <summary>
        /// CoreUser_ChangePassword => ChangePassword
        /// </summary>
        /// <param name="modulePowerTypeName"></param>
        /// <returns></returns>
        public static string GetPowerTypeName(string modulePowerTypeName)
        {
            return GetPowerTypeName(modulePowerTypeName, String.Empty);
        }

        /// <summary>
        /// CoreUser_ChangePassword => ChangePassword
        /// </summary>
        /// <param name="powerType"></param>
        /// <returns></returns>
        public static string GetPowerTypeName(ModulePowerType powerType)
        {
            return GetPowerTypeName(ModulePowerTypeHelper.Power2String(powerType), String.Empty);
        }

        /// <summary>
        /// CoreUser_ChangePassword => ChangePassword
        /// CoreUser_ChangePassword, CoreRole => String.Empty
        /// </summary>
        /// <param name="powerType"></param>
        /// <param name="moduleTypeName"></param>
        /// <returns></returns>
        public static string GetPowerTypeName(ModulePowerType powerType, string moduleTypeName)
        {
            return GetPowerTypeName(ModulePowerTypeHelper.Power2String(powerType), moduleTypeName);
        }

        /// <summary>
        /// CoreUser_ChangePassword => ChangePassword
        /// CoreUser_ChangePassword, CoreRole => String.Empty
        /// </summary>
        /// <param name="modulePowerTypeName"></param>
        /// <param name="moduleTypeName"></param>
        /// <returns></returns>
        public static string GetPowerTypeName(string modulePowerTypeName, string moduleTypeName)
        {
            string powerTypeName = String.Empty;
            int underscoreIndex = modulePowerTypeName.IndexOf('_');
            if (underscoreIndex >= 0)
            {
                string moduleName = modulePowerTypeName.Substring(0, underscoreIndex);
                powerTypeName = modulePowerTypeName.Substring(underscoreIndex + 1);

                // 如果指定的模块前缀不相同
                if (!String.IsNullOrEmpty(moduleTypeName) && moduleName != moduleTypeName)
                {
                    powerTypeName = String.Empty;
                }

            }
            return powerTypeName;
        }

        /// <summary>
        /// 字符串转换为ModulePowerType
        /// </summary>
        /// <param name="text"></param>
        /// <returns></returns>
        public static ModulePowerType String2Power(string text)
        {
            return String2Power(text, false);
        }

        /// <summary>
        /// 字符串转换为ModulePowerType
        /// </summary>
        /// <param name="text"></param>
        /// <param name="ignoreCase"></param>
        /// <returns></returns>
        public static ModulePowerType String2Power(string text, bool ignoreCase)
        {
            if (Enum.IsDefined(typeof(ModulePowerType), text))
            {
                return (ModulePowerType)Enum.Parse(typeof(ModulePowerType), text, ignoreCase);
            }
            else
            {
                return ModulePowerType.None;
            }
        }

        /// <summary>
        /// ModulePowerType转换为字符串
        /// </summary>
        /// <param name="type"></param>
        /// <returns></returns>
        public static String Power2String(ModulePowerType type)
        {
            return type.ToString();
        }

        /// <summary>
        /// 获取所有权限的名称列表
        /// </summary>
        /// <returns></returns>
        public static string[] GetModulePowerTypes()
        {
            return Enum.GetNames(typeof(ModulePowerType));
        }

        private static Dictionary<string, List<string>> _moduleOtherPowerTypeDic = new Dictionary<string, List<string>>();

        /// <summary>
        /// 获得每个模块对应的其他权限名称列表
        /// </summary>
        /// <returns></returns>
        public static List<string> GetModuleOtherPowerTypeDic(string moduleTypeName)
        {
            if (!_moduleOtherPowerTypeDic.ContainsKey(moduleTypeName))
            {
                _moduleOtherPowerTypeDic[moduleTypeName] = new List<string>();

                foreach (string otherPower in GetModulePowerTypes())
                {
                    int underscoreIndex = otherPower.IndexOf('_');
                    if (underscoreIndex >= 0)
                    {
                        string moduleName = otherPower.Substring(0, underscoreIndex);
                        if (moduleTypeName == moduleName)
                        {
                            string powerName = otherPower.Substring(underscoreIndex + 1);
                            _moduleOtherPowerTypeDic[moduleTypeName].Add(powerName);
                        }
                    }
                }

            }

            return _moduleOtherPowerTypeDic[moduleTypeName];
        }


    }

}